import 'package:flutter/material.dart';
import 'package:flutter_color_plugin/flutter_color_plugin.dart';
import 'package:yw_bapp/dao/order_dao.dart';
import 'package:yw_bapp/model/block_money_model.dart';
import 'package:yw_bapp/pages/cash_account_set.dart';
import 'package:yw_bapp/pages/go_cash_page.dart';
import 'package:yw_bapp/pages/withdrawal_list.dart';
import 'package:yw_bapp/provider/store_info_provider.dart';
import 'package:yw_bapp/utils/show_tip_pop.dart';
import 'package:yw_bapp/widgets/custom_route_widget.dart';
import 'package:yw_bapp/widgets/loading_pop_widget.dart';
import 'package:yw_bapp/widgets/optional_widget.dart';
import 'package:yw_bapp/widgets/safe_area_bar_widget.dart';
import 'package:yw_bapp/widgets/text_image_widget.dart';
import 'package:provider/provider.dart';

const textStyle =
    TextStyle(color: Colors.black, fontWeight: FontWeight.bold, fontSize: 30);
final titleStyle = TextStyle(color: ColorUtil.color('#FF666666'), fontSize: 13);

class CashPage extends StatefulWidget {
  @override
  _CashPageState createState() => _CashPageState();
}

class _CashPageState extends State<CashPage> {
  int centBlockNum = 0;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _loadData(context);
  }

  @override
  Widget build(BuildContext context) {
    final int balance = context.watch<StoreInfoProvider>().balance ?? 0;
    return Scaffold(
        appBar: XAppBar(
          statusBarColor: ColorUtil.color('#FFC49226'),
          bgColor: ColorUtil.color('#FFC49226'),
          title: "兑现",
          fun: () {
            
          },
        ),
        body: Container(
          color: Color.fromRGBO(233, 233, 233, 1),
          child: Column(
            children: [
              // 余额展示
              Container(
                padding: EdgeInsets.fromLTRB(0, 24, 0, 24),
                height: 112,
                child: Center(
                  child: bannaceWidget(context, balance),
                ),
              ),
              // 按钮
              OptionalWidget(
                leadingImg: 'images/toCashIcon.png',
                leadingText: '去兑现',
                callBack: () => _toNextPage(context, 1),
              ),
              Divider(
                height: 1,
                color: Color.fromRGBO(233, 233, 233, 1),
              ),
              OptionalWidget(
                leadingImg: 'images/bankCard.png',
                leadingText: '兑现账户设置',
                callBack: () => _toNextPage(context, 2),
              ),
              Container(
                margin: EdgeInsets.only(top: 10),
                child: OptionalWidget(
                  leadingImg: 'images/cashRecords.png',
                  leadingText: '兑现记录',
                  callBack: () => _toNextPage(context, 3),
                ),
              ),
            ],
          ),
        ));
  }

  // 余额展示
  Widget bannaceWidget(BuildContext context, int balance) {
    return Row(
      children: [
        Expanded(
          flex: 1,
          child: Column(
            children: [
              TextWithImage(
                title: '账户余额',
                imageUrl: 'images/bean.png',
                textStyle: titleStyle,
                func: () {},
              ),
              Text(
                '${balance / 100}',
                style: textStyle,
              )
            ],
          ),
        ),
        Expanded(
          flex: 1,
          child: Column(
            children: [
              TextWithImage(
                title: '冻结余额',
                imageUrl: 'images/question_orange.png',
                textStyle: titleStyle,
                imgWdth: 20,
                imgHeight: 20,
                func: () {
                  showAlertDialog(
                    context,
                    '平台用于方便结算的符号',
                    title: '关于开心豆',
                    confirmText: '知道啦',
                    onlyConfirm: true,
                  );
                },
              ),
              Text(
                '${centBlockNum / 100}',
                style: textStyle,
              )
            ],
          ),
        )
      ],
    );
  }

  // 数据加载
  void _loadData(BuildContext context) {
    OrderDao.GetBlockMoney().then((BlockMoneyModel model) => {
          LoadingPop.hide(context),
          if (model.status == 200)
            {
              setState(() {
                centBlockNum = model.data.centBlockNum;
              })
            }
        });
  }

// 点击跳转到指定页面
  void _toNextPage(BuildContext context, int index) {
    switch (index) {
      case 1:
        Navigator.of(context).push(CustomRoute(GoCashPage(), 5));
        LoadingPop.show(context);
        break;
      case 2:
        Navigator.of(context).push(CustomRoute(CashAccountSet(), 5));
        // LoadingPop.show(context);
        break;
      case 3:
        Navigator.of(context).push(CustomRoute(WithdrawalList(), 5));
        LoadingPop.show(context);
        break;
      default:
    }
  }
}
